#!/bin/bash

# Usage:
#
# createElastixWebsite <action>
#
# with
# <action> one of:
#   exportwin
#   exportlinux64
#   exportlinux32 (obsolete)
#   doxygen
#   ziplinux64
#   ziplinux32 (obsolete)
#   zipwin
#   collect
#
# Typical use:
# 1. Prepare a release tag
# 2. Set the version and tag variables in this script
# 3. call this script with argument exportwin
# 4. call this script with argument exportlinux64
# 5. Run cmake on windows and linux.
# 6. run make install on linux, make package, and make package_source
# 7. compile and install on windows using vc2012.
# 8. create the manual on windows, using latex.
# 9. call this script with argument doxygen
# 10. call this script with argument zipwin
# 11. call this script with argument ziplinux64
# 12. call this script with argument collect
# 13. log in on rashindra's computer, using ssh
# 14. call this script with argument exportlinux32 (obsolete)
# 15. run cmake, and make install
# 16. call this script with argument ziplinux32 (osbolete)
# 17. copy the resulting zip manually using winscp to $tempdir/downloads
# 18. copy $tempdir to the website, using winscp.
#
# This script is rather nongeneric. It only works on stefan's computer :)
# 
# Prerequisites: install Dot, doxygen, svn, 7z, cygwin or gitBash (to run this script), cmake, itk 
#

arg="$1"

# You have to set the following
version="4.8"
fullversion="4.8.0"
tag="elastix_04_8"

username="sklein"

# Some base directories
exportbasedirwin="d:/tk/elastix/exports/tagspublic"
#exportbasedirlinux64="m:/tk2/elastix/exports/tagspublic"
# when running locally on linux64 bit machine:
exportbasedirlinux64="/home/sklein/tk/elastix/exports/tagspublic"
# when using rashindra's machine:
exportbasedirlinux32="/home/stefan/tk/elastix/exports/tagspublic"
# when using the biggrid
exportbasedirlinux32grid="m:/tk2/elastix/exports/tagspublic"
exportdirwin="$exportbasedirwin/$tag"
exportdirlinux64="$exportbasedirlinux64/$tag"
# when using the biggrid
exportdirlinux32grid="$exportbasedirlinux32grid/$tag""linux32"
svnurl="https://svn.bigr.nl/elastix"
tempdir="d:/tk/elastixwebsite/$tag"
websitebase="http://elastix.lumc.nl"
copyright="$exportdirwin/src/CopyrightElastix.txt"


# checkout linux64 export:
if [ "$arg" = "exportlinux64" ]
then
  svn export --native-eol LF $svnurl/tagspublic/$tag $exportdirlinux64
  mkdir -p $exportdirlinux64/install
  mkdir -p $exportdirlinux64/release
  mkdir -p $exportdirlinux64/help
  exit
fi

# checkout linux32 export; Run this one directly on a linux32 system!
if [ "$arg" = "exportlinux32" ]
then
  svn export --native-eol LF $svnurl/tagspublic/$tag $exportdirlinux32
  mkdir -p $exportdirlinux32/install
  mkdir -p $exportdirlinux32/release
  mkdir -p $exportdirlinux32/help
  exit
fi

# checkout windows export:
if [ "$arg" = "exportwin" ]
then
  svn export --native-eol CRLF $svnurl/tagspublic/$tag $exportdirwin
  mkdir -p $exportdirwin/install
  mkdir -p $exportdirwin/bin
  mkdir -p $exportdirwin/help
  exit
fi

if [ "$arg" = "doxygen" ]
then
  # Create html help
  currentdir=`pwd`
  cd $exportdirwin/help
  doxygen doxyfile.out &> doxygen.log
  cd $currentdir
  exit
fi

# zip linux64
# todo: does not work anymore, since only access to linux64 via scp currently
# run it locally on the linux system
if [ "$arg" = "ziplinux64" ]
then
  currentdir=`pwd`
  cd $exportdirlinux64
  # tar does not recognise m:/... filenames; convert to cygwin style.
  cwdir=`pwd`
	# rename the tar.bz2 to the website convention
	mv release/elastix-$fullversion-Linux.tar.bz2 release/elastix_linux64_v$version.tar.bz2
	mv release/elastix-$fullversion-Source.tar.bz2 release/elastix_sources_v$version.tar.bz2
	# zip the example. todo: use cpack for this as well.
  cd help
  tar -cjvf $cwdir/release/elastix_example_v$version.tar.bz2 exampleinput example
  cd $currentdir
  exit
fi

# zip linux32grid
if [ "$arg" = "ziplinux32grid" ]
then
  currentdir=`pwd`
  cd $exportdirlinux32grid
  # tar does not recognise m:/... filenames; convert to cygwin style.
  cwdir=`pwd`
	# rename the tar.bz2 to the website convention
	mv release/elastix-$fullversion-Linux.tar.bz2 release/elastix_linux64_v$version.tar.bz2
	# do nothing
  cd $currentdir
  exit
fi

# zip win
# assume this is done on a 64bit system
if [ "$arg" = "zipwin" ]
then
  currentdir=`pwd`
  cd $exportdirwin
  cd bin 
	# call cpack
	cpack -C Release --config CPackConfig.cmake
	cpack -C Release --config CPackSourceConfig.cmake
	# rename zips to website convention
	mv elastix-$fullversion-win64.zip elastix_windows64_v$version.zip
	mv elastix-$fullversion-win64.exe elastix_windows64_v$version.exe
	mv elastix-$fullversion-Source.zip elastix_sources_v$version.zip 
	# create help zip
  cd ../help
	helpzip="elastix_example_v$version.zip"
	rm $exportdirwin/bin/$helpzip
  # changed to use 7z.exe
  #wzzip -ex -a -r -P $exportdirwin/bin/$helpzip exampleinput example.bat
  7z a $exportdirwin/bin/$helpzip exampleinput example.bat
  cd $currentdir
  exit
fi

# collect all stuff that needs to be copied to the website
if [ "$arg" = "collect" ]
then
  rm -r $tempdir
  mkdir -p $tempdir
  mkdir -p $tempdir/download
  cp $exportdirwin/bin/*.zip $tempdir/download
  cp $exportdirwin/bin/*.exe $tempdir/download
  # do manually:
#  cp $exportdirlinux64/release/*.tar.bz2 $tempdir/download
#  cp $exportdirlinux32grid/release/*.tar.bz2 $tempdir/download
  cp $exportdirwin/dox/manual/manual.pdf $tempdir/download/elastix_manual_v$version.pdf
  cp -r $exportdirwin/help/html $tempdir/doxygen

  # Create a version.txt file with the version number in it.
  echo "v"$version > $tempdir/download/version.txt

  # write the date/time
  date '+%d-%m-%Y' > $tempdir/download/latestreleasedate.txt

  # copy the copyright message
  cp $exportdirwin/src/LICENSE $tempdir
  cp $exportdirwin/src/NOTICE $tempdir

  # copy the linux32/64 binaries manually using winscp
  exit
fi

# copy using winscp

exit 0

